package com.rpj.service.impl;

import java.util.List;
import java.util.function.Predicate;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;

import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.rpj.dao.StuDao;
import com.rpj.entity.StuDto;
import com.rpj.entity.StuEntity;
import com.rpj.service.StuService;


@Service
public class StuImpl implements StuService {
	@Autowired
	StuDao studao;

	// ��ѯ����
	public StuEntity findone(StuEntity stu) {
		// TODO Auto-generated method stub
		return studao.getOne(stu.getStuid());
	}

//���
	public StuEntity save(StuEntity stu) {
		// TODO Auto-generated method stub
		return studao.save(stu);
	}

//��ѯȫ��
	public List<StuEntity> queryall() {
		// TODO Auto-generated method stub
		return studao.queryall();
	}

//�޸�
	public int updateStu(String stuname, String stupassword, String sex, String status, String department,
			String specialty, String degree, String stuborrowbooknum, String stuborrowmaxnum, String stugivebooknum,
			String data1,String data2,String data3, String id) {
		// TODO Auto-generated method stub
		return studao.updateStu(stuname, stupassword, sex, status, department, specialty, degree, stuborrowbooknum,
				stuborrowmaxnum, stugivebooknum, data1,data2,data3, id);

	}

	
	@Override // 查询专业
	public List<String> selMajor() {
		// TODO Auto-generated method stub
		return studao.selMajor();
	}

	@Override
	public Page<StuEntity> queryByCondition(StuDto dto, Pageable pageable) {
		Specification<StuEntity> spec = getspec(dto);
		return studao.findAll(spec, pageable);
	}
	public Specification<StuEntity> getspec(final StuDto dto){
		return new Specification<StuEntity>() {

			public javax.persistence.criteria.Predicate toPredicate(Root<StuEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				//创建sql表达式
				javax.persistence.criteria.Predicate p = criteriaBuilder.conjunction();
				//声明表达式集合
				List<Expression<Boolean>> list = p.getExpressions();
				if(dto.getStuname()!=""&&dto.getStuname()!=null) {
					list.add(criteriaBuilder.like(root.get("stuname").as(String.class), "%"+dto.getStuname()+"%"));
				}
				if(dto.getSex()!=""&&dto.getSex()!=null) {
					list.add(criteriaBuilder.like(root.get("sex").as(String.class), "%"+dto.getSex()+"%"));
				}
				if(dto.getData1()!=""&&dto.getData1()!=null) {
					list.add(criteriaBuilder.like(root.get("data1").as(String.class), "%"+dto.getData1()+"%"));
				}
				if(dto.getSpecialty()!=""&&dto.getSpecialty()!=null) {
					list.add(criteriaBuilder.like(root.get("specialty").as(String.class), "%"+dto.getSpecialty()+"%"));
				}
				return p;
			}
		};
	}

	@Override
	public StuEntity stulogin(String stuname, String stupassword) {
		// TODO Auto-generated method stub
		return studao.stulogin(stuname, stupassword);
	}
}
